package com.liuyi.usercenterservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuyi.usercenterservice.common.BaseResponse;
import com.liuyi.usercenterservice.common.ErrorCode;
import com.liuyi.usercenterservice.common.ResultUtils;
import com.liuyi.usercenterservice.entity.domain.Team;
import com.liuyi.usercenterservice.entity.domain.User;
import com.liuyi.usercenterservice.entity.request.*;
import com.liuyi.usercenterservice.entity.vo.TeamVO;
import com.liuyi.usercenterservice.exception.BusinessException;
import com.liuyi.usercenterservice.service.TeamService;
import com.liuyi.usercenterservice.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 刘燚
 * @version v1.0.0
 * @Description TODO
 * @createDate：2024/7/1 21:28
 * @email liuyia2022@163.com
 */
@RestController
@Slf4j
@RequestMapping("/team")
public class TeamController {

    @Resource
    UserService userService;
    @Resource
    TeamService teamService;

    @PostMapping ("/add")
    public BaseResponse<Long> saveTeam(@RequestBody TeamAddRequest teamRequest, HttpServletRequest request){
        if (teamRequest == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        Team team = new Team();
        BeanUtils.copyProperties(teamRequest,team);
        Long id = teamService.addTeam(team,loginUser);
        return ResultUtils.success(id);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> delTeam(@RequestBody TeamDelRequest teamRequest,HttpServletRequest request){
        if (teamRequest.getId() <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        boolean save = teamService.removeByTeamId(teamRequest.getId(),loginUser);
        return ResultUtils.success(save);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updateTeam(@RequestBody TeamUpdateRequest teamRequest,HttpServletRequest request){
        if (teamRequest == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        boolean save = teamService.updateTeam(teamRequest,loginUser);
        return ResultUtils.success(save);
    }
    @GetMapping("/list")
    public BaseResponse<List<TeamVO>> getTeam(TeamQueryRequest queryRequest,HttpServletRequest request){
        if (queryRequest == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        boolean isAdmin = userService.isAdmin(userService.getLoginUser(request));
        List<TeamVO> teams = teamService.listTeam(queryRequest,isAdmin);
        return ResultUtils.success(teams);
    }

    @GetMapping("/list/page")
    public BaseResponse<List<Team>> getTeamByPage(TeamQueryRequest queryRequest){
        if (queryRequest == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        Page<Team> page = new Page<>(queryRequest.getPageNum(),queryRequest.getPageSize());
        Team team = new Team();
        BeanUtils.copyProperties(queryRequest,team);
        return ResultUtils.success(teamService.page(page,new QueryWrapper<>(team)).getRecords());
    }
    @PostMapping("/join")
    public BaseResponse<Boolean> joinTeamByTeamId(@RequestBody TeamJoinRequest joinRequest ,HttpServletRequest request){
        if (joinRequest == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(teamService.joinTeam(joinRequest,loginUser));
    }
    @PostMapping("/quit")
    public BaseResponse<Boolean> quitTeamByTeamId(@RequestBody TeamQuitRequest quitRequest ,HttpServletRequest request){
        if (quitRequest == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(teamService.quitTeam(quitRequest,loginUser));
    }
    @GetMapping("/list/myTeam")
    public BaseResponse<List<TeamVO>> listJoinTeam(HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        List<TeamVO> teams = teamService.listMyTeam(loginUser);
        return ResultUtils.success(teams);
    }

}
